package com.lemote.action.order;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lemote.action.BaseAction;
import com.lemote.core.Page;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.FlowTask;
import com.lemote.entity.OrderEntity;
import com.lemote.entity.PackageBox;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.order.CircuitBoardService;
import com.lemote.service.order.FlowTaskService;
import com.lemote.service.order.OrderService;
import com.lemote.service.order.PackAgeBoxService;
import com.lemote.utils.Constant;

/**
 * @author slaton.wu@gmail.com
 * @version 1.0
 * @created 2011-7-31 下午10:08:05
 */
@Service("packageBoxAction")
public class PackageBoxAction extends BaseAction<PackageBox, PackAgeBoxService> {

	private PackageBox packageBox;

	private Long orderid;
	
	private CircuitBoardService circuitBoardService;
	
	private OrderService orderService;
	
	private List<CircuitBoard> cboards;
	
	private FlowTaskService flowTaskService;
	
	private Integer maxPage;
	
	private String message;
	
	private String error;
	@Override
	public PackageBox getModel() {

		return packageBox;
	}

	@Override
	public String list() throws AppException {
		String orid = request.getParameter("orid");
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		if (orid!=null) {
			orderid = Long.parseLong(orid);
			PropertyFilter filter = new PropertyFilter("EQL_orderEntity.id", String.valueOf(orid));
			filters.add(filter);
			FlowTask task = flowTaskService.getTaskByTaskName("package");
			OrderEntity order = orderService.get(orderid);
			this.maxPage = order.getPacksize();
			this.cboards = circuitBoardService.getPackBoard(orderid, task);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws AppException {
		String oid = request.getParameter("orderid");
		if (oid!=null) {
			orderid = Long.parseLong(oid);
		}
		if (message!=null&&"Y".equals(message)) {
			this.error = "<font style='color:red;font-size:26px;'>*包装箱装满了，请添加新的包装箱！</font>";
		}else{
			this.error="";
		}
		return SUCCESS;
	}

	@Override
	public String save() throws AppException {
		String[] arr= request.getParameterValues("pack");
		String packNo = request.getParameter("packNo");
		User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
		PackageBox box = new PackageBox();
		box.setBoxNo(packNo);
		box.setCreateTime(new Date());
		box.setMaxNum(maxPage);
		box.setNum(arr.length);
		if (orderid!=null) {
			OrderEntity order = orderService.get(orderid);
			order.setCompleteNum(order.getCompleteNum()+arr.length);
			box.setOrderEntity(order);
		}
		box.setPacker(user);
		List<Long> ids = new ArrayList<Long>();
		List<CircuitBoard> circuitBoards = null;
		if (arr!=null&&arr.length>0) {
			for (String str : arr) {
				ids.add(Long.parseLong(str));
			}
			circuitBoards = circuitBoardService.getBoards(ids);
			for (CircuitBoard c : circuitBoards) {
				c.setBox(box);
			}
		}
		box.setBoards(circuitBoards);
		entityService.add(box);
		return SUCCESS;
	}
	
	public String add(){
		String oid = request.getParameter("orderid");
		String packNo = request.getParameter("boxNo");
		if (packNo!=null&&!"".equals(packNo)) {
			PackageBox  b = entityService.get("boxNo", packNo);
			if (b!=null) {
				error = "箱号"+packNo+"已经添加过了！！";
				return "addPack";
			}else {
				error="";
			}
		}
		
		
		if (oid!=null&&!"".equals(oid)&&packNo!=null&&!"".equals(packNo)) {
			OrderEntity order = orderService.get(Long.parseLong(oid));
			if (order!=null) {
				List<PackageBox> boxes = entityService.getPackAgeBoxList(order);
				if (order.getPackNum()<=boxes.size()) {
					error = "最多只能添加"+order.getPackNum()+"箱哦！！";
					return "addPack";
				}
				User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
				PackageBox box = new PackageBox();
				box.setBoxNo(packNo);
				box.setCreateTime(new Date());
				box.setMaxNum(order.getPacksize());
				box.setNum(0);
				box.setOrderEntity(order);
				box.setPacker(user);
				entityService.add(box);
				packageBox = box;
			}
		}
		return SUCCESS;
	}
	
	public String addboard(){
		String boxNo = request.getParameter("boxNo");
		String cno = request.getParameter("cno");
		if (cno!=null&&!"".equals(StringUtils.trim(cno))) {
			CircuitBoard board = circuitBoardService.get("icNo",cno);
			OrderEntity orderEntity = null;
			if (board!=null) {
				orderEntity = board.getOrderEntity();
				if (board.getBox()==null) {
					if ("package".equals(board.getFlowTask().getTaskEname())) {
						if (boxNo!=null&&!"".equals(StringUtils.trim(boxNo))) {
							PackageBox box = entityService.get("boxNo",boxNo);
							if (box!=null) {
								OrderEntity oe = board.getOrderEntity();
									if (oe.getOrderNo().equals(board.getOrderNo())) {
										if (oe.getPacksize()>box.getNum()) {
											List<CircuitBoard> boards = box.getBoards();
											boards.add(board);
											orderEntity.setCompleteNum(orderEntity.getCompleteNum());
											board.setBox(box);
											box.setBoards(boards);
											box.setNum(box.getNum()+1);
											entityService.update(box);
											orderService.update(orderEntity);
											this.packageBox = box;
											message = "<font style='font-size:26px;'>主板已通过功能测试，包装成功！</font>";
									}else{
										message = "Y";
										this.orderid = box.getOrderEntity().getId();
										return "addPack";
									}
								}else{
									message = "<font style='color:red;font-size:26px;'>*在订单"+oe.getOrderNo()+"中不存在编号为"+cno+"的主板!</font>";
									return SUCCESS;
									
								}
							}else{
								message = "<font style='color:red;font-size:26px;'>*没有找到编号为 "+boxNo+"包装箱！</font>";
							}
						}
					}else{
						message = "<font style='color:red;font-size:26px;'>*主板还没有通过功能测试，暂时不能包装！</font>";
					}
					
				}else {
					message="<font style='color:red;font-size:26px;'>*主板"+cno+"已经包装在箱号为"+board.getBox().getBoxNo()+"中了!</font>";
				}
			}else{
				message = "<font style='color:red;font-size:26px;'>*不存在编号为"+cno+"的主板!</font>";
				return SUCCESS;
			}
			
			List<CircuitBoard> boards = orderEntity.getBoards();
			boolean endOrder = true;
			for (CircuitBoard cb : boards) {
				if (("0".equals(cb.getStatus())||"1".equals(cb.getStatus()))&&cb.getBox()==null) {
					endOrder = false;
					break;
				}
			}
			if (endOrder) {
				orderEntity.setStatus("2");
				message="<font style='color:green;font-size:26px'>订单生成完成</font>";
				orderService.update(orderEntity);
			}
			
		}
		return SUCCESS;
	}
	
	public String toAddBoard(){
		String boxNo = request.getParameter("boxNo");
		if (boxNo!=null) {
			packageBox = entityService.get("boxNo", boxNo);
		}
		message = "";
		return SUCCESS;
	}

	@Override
	public String delete() throws AppException {

		return null;
	}

	@Override
	public void prepareModel() throws AppException {
		if (id != null) {
			packageBox = entityService.get(id);
		} else {
			packageBox = new PackageBox();
		}

	}

	@Autowired
	@Override
	public void setEntityService(PackAgeBoxService entityService) {

		this.entityService = entityService;
	}

	public PackageBox getPackageBox() {
		return packageBox;
	}

	public void setPackageBox(PackageBox packageBox) {
		this.packageBox = packageBox;
	}

	public Long getOrderid() {
		return orderid;
	}
	public void setOrderid(Long orderid) {
		this.orderid = orderid;
	}

	@Autowired
	public void setCircuitBoardService(CircuitBoardService circuitBoardService) {
		this.circuitBoardService = circuitBoardService;
	}

	
	public List<CircuitBoard> getCboards() {
		return cboards;
	}


	public void setCboards(List<CircuitBoard> cboards) {
		this.cboards = cboards;
	}

	@Autowired
	public void setFlowTaskService(FlowTaskService flowTaskService) {
		this.flowTaskService = flowTaskService;
	}

	public Integer getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(Integer maxPage) {
		this.maxPage = maxPage;
	}

	@Autowired
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}
	
	

}
